using RxjhServer.DbClss;
using RxjhServer.HelperTools;
using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace RxjhServer
{
	public class Form14 : Form
	{
		private IContainer components = null;

		private GroupBox groupBox1;

		private TextBox textBox2;

		private TextBox textBox1;

		private Button button1;

		private Label label2;

		private Label label1;

		public Form14()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			try
			{
				button1.Enabled = false;
				button1.Text = "请稍等....";
				foreach (Players value in World.allConnectedChars.Values)
				{
					if (value.离线挂机打怪模式 == 0)
					{
						if (World.换线存档方式 == 0)
						{
							if (!value.保存人物数据RightNow())
							{
								Form1.WriteLine(3, "人物换线存档[" + value.Userid + "]-[" + value.UserName + "] 失败");
								return;
							}
						}
						else
						{
							value.保存人物的数据();
							value.初始化标志 = false;
							Thread.Sleep(1);
							while (World.SqlPool.Count > 0)
							{
								Thread.Sleep(5);
							}
						}
						Thread.Sleep(100);
						换线(textBox1.Text, int.Parse(textBox2.Text), value);
					}
				}
				button1.Enabled = true;
				button1.Text = "全体转移";
				MessageBox.Show("所有在线玩家转移完成!!!");
			}
			catch
			{
				MessageBox.Show("换线过程中有错误请重新运行换线");
			}
		}

		public void 换线(string IP, int port, Players play)
		{
			try
			{
				string sqlCommand = "select * from [TBL_人物数据] where FLD_NAME=@Userid";
				SqlParameter[] prams = new SqlParameter[1]
				{
					SqlDBA.MakeInParam("@Userid", SqlDbType.VarChar, 30, play.UserName)
				};
				DataTable dBToDataTable = DBA.GetDBToDataTable(sqlCommand, prams);
				if (dBToDataTable == null || dBToDataTable.Rows.Count == 0)
				{
					if (play.Client != null)
					{
						Form1.WriteLine(1, "获取人物出错，[" + play.Userid + "][" + play.UserName + "]");
						play.OpClient(1);
						play.Client.Dispose();
					}
				}
				else
				{
					string hex = "AA550F00000003501500000000000000000AAE55AA";
					byte[] array = Converter.hexStringToByte(hex);
					Buffer.BlockCopy(BitConverter.GetBytes(play.人物全服ID), 0, array, 5, 2);
					if (play.Client != null)
					{
						play.Client.Send(array, array.Length);
					}
					play.Logout();
					Thread.Sleep(3000);
					play.Client.在线 = false;
					play.退出中 = true;
					int value = (int)dBToDataTable.Rows[0]["FLD_INDEX"];
					dBToDataTable.Dispose();
					string hex2 = "AA553700055013D20028000100000000000000000000000000000000000000000000003200000000000000000000000000000000000000000023BC55AA";
					byte[] array2 = Converter.hexStringToByte(hex2);
					Buffer.BlockCopy(BitConverter.GetBytes(World.服务器ID), 0, array2, 15, 4);
					Buffer.BlockCopy(BitConverter.GetBytes(value), 0, array2, 19, 4);
					Buffer.BlockCopy(BitConverter.GetBytes(port), 0, array2, 31, 4);
					byte[] bytes = Encoding.Default.GetBytes(IP);
					Buffer.BlockCopy(bytes, 0, array2, 35, bytes.Length);
					if (play.Client != null)
					{
						play.Client.Send(array2, array2.Length);
					}
					World.conn.发送("用户换线通知|" + play.Userid);
				}
			}
			catch
			{
			}
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing && components != null)
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		private void InitializeComponent()
		{
			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(RxjhServer.Form14));
			groupBox1 = new System.Windows.Forms.GroupBox();
			textBox2 = new System.Windows.Forms.TextBox();
			textBox1 = new System.Windows.Forms.TextBox();
			button1 = new System.Windows.Forms.Button();
			label2 = new System.Windows.Forms.Label();
			label1 = new System.Windows.Forms.Label();
			groupBox1.SuspendLayout();
			SuspendLayout();
			groupBox1.Controls.Add(textBox2);
			groupBox1.Controls.Add(textBox1);
			groupBox1.Controls.Add(button1);
			groupBox1.Controls.Add(label2);
			groupBox1.Controls.Add(label1);
			groupBox1.Location = new System.Drawing.Point(12, 12);
			groupBox1.Name = "groupBox1";
			groupBox1.Size = new System.Drawing.Size(270, 131);
			groupBox1.TabIndex = 0;
			groupBox1.TabStop = false;
			groupBox1.Text = "在线人员集体跨线";
			textBox2.Location = new System.Drawing.Point(117, 47);
			textBox2.Name = "textBox2";
			textBox2.Size = new System.Drawing.Size(100, 21);
			textBox2.TabIndex = 4;
			textBox1.Location = new System.Drawing.Point(117, 20);
			textBox1.Name = "textBox1";
			textBox1.Size = new System.Drawing.Size(100, 21);
			textBox1.TabIndex = 3;
			button1.Location = new System.Drawing.Point(117, 91);
			button1.Name = "button1";
			button1.Size = new System.Drawing.Size(100, 23);
			button1.TabIndex = 2;
			button1.Text = "全体转移";
			button1.UseVisualStyleBackColor = true;
			button1.Click += new System.EventHandler(button1_Click);
			label2.AutoSize = true;
			label2.Location = new System.Drawing.Point(21, 50);
			label2.Name = "label2";
			label2.Size = new System.Drawing.Size(77, 12);
			label2.TabIndex = 1;
			label2.Text = "其他线路端口";
			label1.AutoSize = true;
			label1.Location = new System.Drawing.Point(21, 26);
			label1.Name = "label1";
			label1.Size = new System.Drawing.Size(65, 12);
			label1.TabIndex = 0;
			label1.Text = "其他线路IP";
			base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
			base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			base.ClientSize = new System.Drawing.Size(294, 159);
			base.Controls.Add(groupBox1);
			base.Icon = (System.Drawing.Icon)resources.GetObject("$this.Icon");
			base.Name = "Form14";
			Text = "全体跨线";
			base.Load += new System.EventHandler(Form14_Load);
			groupBox1.ResumeLayout(false);
			groupBox1.PerformLayout();
			ResumeLayout(false);
		}

		private void Form14_Load(object sender, EventArgs e)
		{
		}
	}
}
